package com.tk.exam.utils;

import com.tk.exam.config.properties.SystemProperties;
import com.tk.exam.domain.TkToken;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.Date;
import java.util.UUID;

@Component
public class SSOUtil {

    @Autowired
    SystemProperties systemProperties;

    public static String generateSid() {
        return UUID.randomUUID().toString().replaceAll("-", "");
    }

    public TkToken generateToken(String tokenInfo) {
        ZoneId zone = ZoneId.systemDefault();
        LocalDateTime localDateTime = LocalDateTime.now();

        TkToken tkToken = new TkToken();
        tkToken.setToken(generateSid());
        tkToken.setTokenInfo(tokenInfo);
        tkToken.setIsExpired((byte) 0);
        tkToken.setCreateTime(Date.from(localDateTime.atZone(zone).toInstant()));
        tkToken.setUpdateTime(Date.from(localDateTime.atZone(zone).toInstant()));
        localDateTime=localDateTime.plusHours(Integer.parseInt(systemProperties.getTokenExpireTime()));
        tkToken.setExpireTime(Date.from(localDateTime.atZone(zone).toInstant()));

        return tkToken;
    }

}
